﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shared.Model;
using Client;
using ManageSystem.Dialog;

namespace ManageSystem.Manager
{
    public partial class FormFlow : UserControl
    {
        private List<FLOW_GROUP> _listGroup = null;//流程集集合
        private List<FLOW> _listFlow = null;//流程集合
        private FLOW _selectedFlow = null;//选择的流程
        private FLOW_GROUP _group = null;//选择的流程集集合

        public FormFlow()
        {
            InitializeComponent();
        }
        //加载流程集合
        private void FormFlow_Load(object sender, EventArgs e)
        {
            FLOW_GROUP group = new FLOW_GROUP();
            var selectGroup = MethodWcf.SelectFlowGroupClient(group);
            if (selectGroup.result)
            {
                _listGroup = selectGroup.value;
                for (int i = 0; i < _listGroup.Count; i++)
                {
                    this.comboBoxFlowGroup.Items.Add(_listGroup[i].NAME);
                }
            }
        }
        //加载流程集合中的流程
        private void comboBoxFlowGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获得集合的编码
            if (_listGroup.Count == 0)
            {
                return;
            }
            //获取集合的名称
            string groupName = comboBoxFlowGroup.SelectedItem.ToString();

            //清空流程
            this.listViewFlow.Items.Clear();

            //MessageBox.Show(groupName);
            //根据选择的流程集合的名称获得流程的ID
             _group = (from g in _listGroup
                         where g.NAME == groupName
                         select g).FirstOrDefault();

            if (_group == null)
            {
                MessageBox.Show("未能获得流程集合的ID");
                return;
            }

            //根据groupID获得相关的流程表
            FLOW flow = new FLOW();
            flow.SetWhere();
            flow.FLOW_GROUP_ID = _group.ID;

            var selectFlow = MethodWcf.SelectFlowClient(flow);
            if (selectFlow.result)
            {
                _listFlow = (from f in selectFlow.value
                             orderby f.SORT
                             select f).ToList();

                for (int i = 0; i < _listFlow.Count; i++)
                {
                    SetListViewFlow(_listFlow[i]);
                }
            }
        }
        //设置产品信息到LISTVIEW中
        private void SetListViewFlow(FLOW flow)
        {
            //在ListViewFlow中加入流程信息
            ListViewItem item = new ListViewItem();
            item.Text = flow.ID.ToString();
            item.SubItems.Add(flow.NAME);
            item.SubItems.Add(flow.CONTEXT);
            if (flow.NODE_START != null)
                item.SubItems.Add(flow.NODE_START.ToString());
            else
                item.SubItems.Add("");

            if (flow.NODE_END != null)
                item.SubItems.Add(flow.NODE_END.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(flow.NODE_ALL);
            if (flow.SORT != null)
                item.SubItems.Add(flow.SORT.ToString());
            else
                item.SubItems.Add("");

            if (flow.FLOW_GROUP_ID != null)
                item.SubItems.Add(flow.FLOW_GROUP_ID.ToString());
            else
                item.SubItems.Add("");

            this.listViewFlow.Items.Add(item);
        }
        /// <summary>
        /// 显示流程的信息
        /// </summary>
        private void ClearText()
        {
            this.textFlowId.Clear();
            this.textFlowName.Clear();
            this.textFlowContext.Clear();
            this.textFlowNodeStart.Clear();
            this.textFlowNodeEnd.Clear();
            this.textFlowNodeAll.Clear();
        }
        /// <summary>
        /// 设置流程信息
        /// </summary>
        /// <param name="flow"></param>
        private void SetFlowText(FLOW flow)
        {
            this.textFlowId.Text = flow.ID.ToString();
            this.textFlowName.Text = flow.NAME;
            this.textFlowContext.Text = flow.CONTEXT;
            if (flow.NODE_START != null)
                this.textFlowNodeStart.Text = flow.NODE_START.ToString();
            if (flow.NODE_END != null)
                this.textFlowNodeEnd.Text = flow.NODE_END.ToString();
            this.textFlowNodeAll.Text = flow.NODE_ALL;
        }
        //显示信息
        private void listViewFlow_MouseClick(object sender, MouseEventArgs e)
        {
            ClearText();
            if (this._listFlow.Count == 1)
            {
                _selectedFlow = _listFlow[0];
                SetFlowText(_selectedFlow);
            }

            var selected = this.listViewFlow.SelectedItems;
            if (this.listViewFlow.Items.Count >= 1 && selected.Count == 1)//ITEM的数量大于1
            {
                _selectedFlow = (from f in _listFlow
                                 where f.ID == long.Parse(selected[0].Text)
                                 select f).FirstOrDefault();

                SetFlowText(this._selectedFlow);
            }
        }
        //修改工序
        private void buttonFlowUpdate_Click(object sender, EventArgs e)
        {
            if (this._selectedFlow == null)//没有选中工序
            {
                MessageBox.Show("没有选中流程");
                return;
            }

            DialogNodeUpdate node = new DialogNodeUpdate(_selectedFlow);
            node.ShowDialog();

            //更新流程集合中的流程信息
            this.comboBoxFlowGroup_SelectedIndexChanged(null, null);
        }
        //添加流程
        private void buttonFlowAdd_Click(object sender, EventArgs e)
        {
            string name = this.textFlowName.Text;
            string context = this.textFlowContext.Text;

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(context))
            {
                MessageBox.Show("流程名称或描述为空");
                return;
            }

            if (_group == null)
            {
                MessageBox.Show("流程集合没有选择");
                return;
            }

            try
            {
                int sort = 1;
                //获得流程顺序号码
                if (_listFlow.Count >= 1)
                {
                    sort = (from f in _listFlow
                            select f.SORT.Value).Max() + 1;
                }
                //插入新的流程数据
                var flow = new FLOW();
                flow.SetUpdate();
                flow.CONTEXT = context;
                flow.NAME = name;
                flow.SORT = sort;
                flow.FLOW_GROUP_ID = _group.ID;

                var insertFlow = MethodWcf.InsertFlowClient(flow);//写入数据
                if (insertFlow.result)
                {
                    MessageBox.Show(string.Format("写入流程成功，ID为{0}", insertFlow.message));
                    //更新流程信息
                    flow.Clear();
                    flow.SetWhere();
                    flow.FLOW_GROUP_ID = _group.ID;
                    var selectFlow = MethodWcf.SelectFlowClient(flow);
                    if (selectFlow.result)
                    {
                        comboBoxFlowGroup_SelectedIndexChanged(null, null);
                    }
                }
                else
                {
                    MessageBox.Show(insertFlow.message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("插入数据失败，错误代码为：\n{0}", ex.Message));
            }     
        }
        //流程上移
        private void buttonUp_Click(object sender, EventArgs e)
        {
            if (_listFlow==null||_listFlow.Count <= 1) return;//当流程数量为1时，上移操作无效
            var selected = this.listViewFlow.SelectedItems;

            if (selected.Count != 1)
            {
                MessageBox.Show("只能选择一行移动，操作无效");
                return;
            }

            //获取选中的行号
            int choiceRow = selected[0].Index;
            if (choiceRow == 0)
            {
                MessageBox.Show("选中的是第一行，上移操作无效");
                return;
            }

            long choiceId = long.Parse(selected[0].Text);
            long lastId = long.Parse(this.listViewFlow.Items[choiceRow - 1].Text);

            //获得当前行和之前行的流程信息
            var choice = (from f in _listFlow
                          where f.ID == choiceId
                          select f).FirstOrDefault();

            var last = (from f in _listFlow
                        where f.ID == lastId
                        select f).FirstOrDefault();

            choice.SetWhere();
            choice.ID = choice.ID;
            last.SetWhere();
            last.ID = last.ID;

            int sortTemp = choice.SORT.Value;
            choice.SetUpdate();
            choice.SORT = last.SORT;

            last.SetUpdate();
            last.SORT = sortTemp;

            //更新两者的顺序
            var updateChoice = MethodWcf.UpdateFlowClient(choice);
            var updateLast = MethodWcf.UpdateFlowClient(last);

            if (updateChoice.result==false || updateLast.result==false)
            {
                MessageBox.Show("交换两者顺序失败");
                return;
            }

            ClearText();
            comboBoxFlowGroup_SelectedIndexChanged(null, null);
        }
        //流程下移
        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (_listFlow==null||_listFlow.Count <= 1) return;//当流程数量为1时，上移操作无效

            var selected = this.listViewFlow.SelectedItems;

            if (selected.Count != 1)
            {
                MessageBox.Show("只能选择一行移动，操作无效");
                return;
            }

            //获取选中的行号
            int choiceRow = selected[0].Index;
            if (choiceRow == this.listViewFlow.Items.Count-1)
            {
                MessageBox.Show("选中的是最后一行，下移动操作无效");
                return;
            }

            long choiceId = long.Parse(selected[0].Text);
            long nextId = long.Parse(this.listViewFlow.Items[choiceRow + 1].Text);

            //获得当前行和之前行的流程信息
            var choice = (from f in _listFlow
                          where f.ID == choiceId
                          select f).FirstOrDefault();

            var next = (from f in _listFlow
                        where f.ID == nextId
                        select f).FirstOrDefault();

            choice.SetWhere();
            choice.ID = choice.ID;
            next.SetWhere();
            next.ID = next.ID;

            int sortTemp = choice.SORT.Value;
            choice.SetUpdate();
            choice.SORT = next.SORT;

            next.SetUpdate();
            next.SORT = sortTemp;

            //更新两者的顺序
            var updateChoice = MethodWcf.UpdateFlowClient(choice);
            var updateNext = MethodWcf.UpdateFlowClient(next);

            if (updateChoice.result == false || updateNext.result == false)
            {
                MessageBox.Show("交换两者顺序失败");
                return;
            }

            ClearText();
            comboBoxFlowGroup_SelectedIndexChanged(null, null);
        }
        //鼠标双击操作，进入修改环节
        private void listViewFlow_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            buttonFlowUpdate_Click(null, null);
        }
    }
}
